package uy.edu.um.ltic.client.communication;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;

import uy.edu.um.ltic.commons.*;
import uy.edu.um.ltic.exception.DataBaseException;


public class GeneralEntitiesRemoteClientMgr implements GeneralEntitiesRemoteClientMgt {
	
	private static GeneralEntitiesRemoteClientMgr instance = null;

	private GeneralEntitiesRemoteClientMgr() {
	}

	public static GeneralEntitiesRemoteClientMgr getInstance() {
		if (instance == null) {
			instance = new GeneralEntitiesRemoteClientMgr();
		}
		return instance;
	}
	
	@Override
	public boolean addCountry(String country) throws ExceptionComm, DataBaseException {
		boolean ok = false;
		if (UtilUs.veriNom(country)){
		
			String sObjectService = "GeneralEntitiesMgt"; 
			Registry oRegitry;
			try {
				oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
				GeneralEntitiesRemoteMgt oGeneralEntitiesMgtRemoteMgt = (GeneralEntitiesRemoteMgt) 
						oRegitry.lookup(sObjectService); 
				oGeneralEntitiesMgtRemoteMgt.addCountry(country);
				ok = true;
			
			} catch (RemoteException | NotBoundException e) {
					throw new ExceptionComm(e);	
			
			} 
		}else{
			ok = false;
		}
		
		return ok;
	}

	@Override
	public boolean addPositionInCompany(String positionInCompany)
			throws ExceptionComm, DataBaseException {
		boolean ok = false;
		if (UtilUs.veriNom(positionInCompany)){
		
			String sObjectService = "GeneralEntitiesMgt"; 
			Registry oRegitry;
			try {
				oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
				GeneralEntitiesRemoteMgt oGeneralEntitiesMgtRemoteMgt = (GeneralEntitiesRemoteMgt) 
						oRegitry.lookup(sObjectService); 
				oGeneralEntitiesMgtRemoteMgt.addPositionInCompany(positionInCompany);
				ok = true;
			
			} catch (RemoteException | NotBoundException e) {
					throw new ExceptionComm(e);	
			
			} 
		}else{
			ok = false;
		}
		
		return ok;
	}

	@Override
	public boolean addBranch(String branch) throws ExceptionComm, DataBaseException {
		boolean ok = false;
		if (UtilUs.veriNom(branch)){
		
			String sObjectService = "GeneralEntitiesMgt"; 
			Registry oRegitry;
			try {
				oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
				GeneralEntitiesRemoteMgt oGeneralEntitiesMgtRemoteMgt = (GeneralEntitiesRemoteMgt) 
						oRegitry.lookup(sObjectService); 
				oGeneralEntitiesMgtRemoteMgt.addBranch(branch);
				ok = true;
			
			} catch (RemoteException | NotBoundException e) {
					throw new ExceptionComm(e);	
			
			} 
		}else{
			ok = false;
		}
		
		return ok;
		
	}

	@Override
	public boolean addDepartment(String department) throws ExceptionComm, DataBaseException {
		boolean ok = false;
		if (UtilUs.veriNom(department)){
		
			String sObjectService = "GeneralEntitiesMgt"; 
			Registry oRegitry;
			try {
				oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
				GeneralEntitiesRemoteMgt oGeneralEntitiesMgtRemoteMgt = (GeneralEntitiesRemoteMgt) 
						oRegitry.lookup(sObjectService); 
				oGeneralEntitiesMgtRemoteMgt.addDepartment(department);
				ok = true;
			
			} catch (RemoteException | NotBoundException e) {
					throw new ExceptionComm(e);	
			
			} 
		}else{
			ok = false;
		}
		
		return ok;
	}

	@Override
	public void deleteCountry(String country) throws ExceptionComm, DataBaseException {
		String sObjectService = "GeneralEntitiesMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			GeneralEntitiesRemoteMgt oGeneralEntitiesMgtRemoteMgt = (GeneralEntitiesRemoteMgt) 
					oRegitry.lookup(sObjectService); 
			oGeneralEntitiesMgtRemoteMgt.deleteCountry(country);
			
		
		} catch (RemoteException | NotBoundException e) {
				throw new ExceptionComm(e);	
		
		} 
	}

	@Override
	public void deletePositionInCompany(String positionInCompany)
			throws ExceptionComm, DataBaseException {
		String sObjectService = "GeneralEntitiesMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			GeneralEntitiesRemoteMgt oGeneralEntitiesMgtRemoteMgt = (GeneralEntitiesRemoteMgt) 
					oRegitry.lookup(sObjectService); 
			oGeneralEntitiesMgtRemoteMgt.deletePositionInCompany(positionInCompany);
			
		
		} catch (RemoteException | NotBoundException e) {
				throw new ExceptionComm(e);	
		
		} 
	}

	@Override
	public void deleteBranch(String branch) throws ExceptionComm, DataBaseException {
		String sObjectService = "GeneralEntitiesMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			GeneralEntitiesRemoteMgt oGeneralEntitiesMgtRemoteMgt = (GeneralEntitiesRemoteMgt) 
					oRegitry.lookup(sObjectService); 
			oGeneralEntitiesMgtRemoteMgt.deleteBranch(branch);
			
		
		} catch (RemoteException | NotBoundException e) {
				throw new ExceptionComm(e);	
		
		} 
	}

	@Override
	public void deleteDepartment(String department) throws ExceptionComm, DataBaseException {
		String sObjectService = "GeneralEntitiesMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			GeneralEntitiesRemoteMgt oGeneralEntitiesMgtRemoteMgt = (GeneralEntitiesRemoteMgt) 
					oRegitry.lookup(sObjectService); 
			oGeneralEntitiesMgtRemoteMgt.deleteDepartment(department);
			
		
		} catch (RemoteException | NotBoundException e) {
				throw new ExceptionComm(e);	
		
		} 
	}

	@Override
	public ArrayList<String> getCountries() throws ExceptionComm, DataBaseException {
		ArrayList<String> oListCountry;
		String sObjectService = "GeneralEntitiesMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			GeneralEntitiesRemoteMgt oGeneralEntitiesMgtRemoteMgt = (GeneralEntitiesRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oListCountry = oGeneralEntitiesMgtRemoteMgt.getCountries();
			
		} catch (RemoteException | NotBoundException e) {
		throw new ExceptionComm(e);	
		} 
		return oListCountry;
		
	}

	@Override
	public ArrayList<String> getPositionsInCompany() throws ExceptionComm, DataBaseException {
		ArrayList<String> oListPositionsInCompany;
		String sObjectService = "GeneralEntitiesMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			GeneralEntitiesRemoteMgt oGeneralEntitiesMgtRemoteMgt = (GeneralEntitiesRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oListPositionsInCompany = oGeneralEntitiesMgtRemoteMgt.getPositionsInCompany();
			
		} catch (RemoteException | NotBoundException e) {
		throw new ExceptionComm(e);	
		} 
		return oListPositionsInCompany;
		
	}

	@Override
	public ArrayList<String> getBranches() throws ExceptionComm, DataBaseException {
		ArrayList<String> oListBranches;
		String sObjectService = "GeneralEntitiesMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			GeneralEntitiesRemoteMgt oGeneralEntitiesMgtRemoteMgt = (GeneralEntitiesRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oListBranches = oGeneralEntitiesMgtRemoteMgt.getBranches();
			
		} catch (RemoteException | NotBoundException e) {
		throw new ExceptionComm(e);	
		} 
		return oListBranches;
	}

	@Override
	public ArrayList<String> getDepartments() throws ExceptionComm, DataBaseException {
		ArrayList<String> oListDepartments;
		String sObjectService = "GeneralEntitiesMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			GeneralEntitiesRemoteMgt oGeneralEntitiesMgtRemoteMgt = (GeneralEntitiesRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oListDepartments = oGeneralEntitiesMgtRemoteMgt.getDepartments();
			
		} catch (RemoteException | NotBoundException e) {
		throw new ExceptionComm(e);	
		} 
		return oListDepartments;
	}

	@Override
	public void dropConnection(String nEmp) throws ExceptionComm {
		String sObjectService = "GeneralEntitiesMgt"; 
		Registry oRegitry;
		try {
			oRegitry = LocateRegistry.getRegistry(ServerIp.sIP,65535);
			GeneralEntitiesRemoteMgt oGeneralEntitiesMgtRemoteMgt = (GeneralEntitiesRemoteMgt) 
			oRegitry.lookup(sObjectService); 
			oGeneralEntitiesMgtRemoteMgt.dropConnection(nEmp);
			
		} catch (RemoteException | NotBoundException e) {
		throw new ExceptionComm(e);	
		} 
	}

}
